﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Xml;
using System.Xml.Serialization;

namespace SusyGame_Model
{
    [Serializable]
    public class Map
    {
        //Atributos
        private String mapName;
        private int mapType;
        private int[,] mapV;
        private int alto, ancho;
        [NonSerialized]
        private List<Texture2D> tileTextures = new List<Texture2D>();
        private Queue<Vector2> waypoints0 = new Queue<Vector2>();
        private Queue<Vector2> waypoints1 = new Queue<Vector2>();
        private Queue<Vector2> waypoints2 = new Queue<Vector2>();
        private Queue<Vector2> waypoints6 = new Queue<Vector2>();
        private Queue<Vector2> waypoints7 = new Queue<Vector2>();
        private Queue<Vector2> waypoints8 = new Queue<Vector2>();

        public Queue<Vector2> Waypoints1
        {
            get { return waypoints1; }
            set { waypoints1 = value; }
        }

        public Queue<Vector2> Waypoints2
        {
            get { return waypoints2; }
            set { waypoints2 = value; }
        }
        
        public Queue<Vector2> Waypoints6
        {
            get { return waypoints6; }
            set { waypoints6 = value; }
        }
        
        public Queue<Vector2> Waypoints7
        {
            get { return waypoints7; }
            set { waypoints7 = value; }
        }

        public Queue<Vector2> Waypoints8
        {
            get { return waypoints8; }
            set { waypoints8 = value; }
        }

        //Propiedades
        public String MapName
        {
            get { return mapName; }
            set { mapName = value; }
        }
        
        public int MapType
        {
            get { return mapType; }
            set { mapType = value; }
        }
        
        public int[,] MapV
        {
            get { return mapV; }
            set { mapV = value; }
        }
       
        public int Ancho
        {
            get { return ancho; }
            set { ancho = value; }
        }

        public int Alto
        {
            get { return alto; }
            set { alto = value; }
        }

        public void AddTexture(Texture2D texture)
        {
            tileTextures.Add(texture);
        }

        public Queue<Vector2> Waypoints0
        {
            get { return waypoints0; }
            set { waypoints0 = value; }
        }

        //Constructores
        public Map()
        {
        }

        public Map(int mapType)
        {
            //Las dimensiones del mapa seran fijas
            this.MapType = mapType;
            this.Alto = 9;
            this.Ancho = 8;

            this.MapV = new int[alto, ancho];

            //Leemos el mapa de un archivo
            String line;
            int tamanho = 0, filas = 0;
            try
            {
                using (StreamReader sr = new StreamReader("Mapas/Mapa.txt"))
                {
                    while ((line = sr.ReadLine()) != null)
                    {
                        tamanho = line.Length;
                        for (int i = 0; i < tamanho; i++)
                        {
                            mapV[filas, i] = int.Parse(line[i].ToString());
                        }
                        filas++;
                    }
                }
            }
            catch (Exception exe)
            {
                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(exe.Message);
            }

            if (mapType == 1)
            {//Mapa sin agua
                this.MapName = "Mapa sin agua";                                                  
            }
            else
            {
                //Asigna el nombre
                this.MapName = "Mapa con agua";
            }

            waypoints0.Enqueue(new Vector2(8*150, 0*80));
            waypoints0.Enqueue(new Vector2(0*150, 0*80));

            waypoints1.Enqueue(new Vector2(8 * 150, 1 * 80));
            waypoints1.Enqueue(new Vector2(0 * 150, 1 * 80));

            waypoints2.Enqueue(new Vector2(8 * 150, 2 * 80));
            waypoints2.Enqueue(new Vector2(0 * 150, 2 * 80));

            waypoints6.Enqueue(new Vector2(8 * 150, 6 * 80));
            waypoints6.Enqueue(new Vector2(0 * 150, 6 * 80));

            waypoints7.Enqueue(new Vector2(8 * 150, 7 * 80));
            waypoints7.Enqueue(new Vector2(0 * 150, 7 * 80));

            waypoints8.Enqueue(new Vector2(8 * 150, 8 * 80));
            waypoints8.Enqueue(new Vector2(0 * 150, 8 * 80));
        }

        //Metodo para imprimir el mapa
        public void PrintMap(SpriteBatch batch)
        {
            for (int x = 0; x < Ancho; x++)
            {
                for (int y = 0; y < Alto; y++)
                {
                    int textureIndex = mapV[y, x];
                    if (textureIndex == -1)
                        continue;

                    Texture2D texture = tileTextures[textureIndex];
                    batch.Draw(texture, new Rectangle(
                        x * 150, y * 80, 150, 80), Color.White);
                }
            }        
        }

        public int GetIndex(int cellX, int cellY)
        {
            if (cellX < 0 || cellX > this.ancho - 1 || cellY < 0 || cellY > this.alto - 1)
                return 0;

            return mapV[cellY, cellX];
        }
    } 	
}
